package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.feign.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Override
    public Map getItem(Long skuId) {
        Map result = new HashMap();

        //构建异步编排
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //SKuInfo
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            System.out.println("skuInfo:" + Thread.currentThread().getName());
            result.put("skuInfo",skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(t -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(t.getCategory3Id());
            System.out.println("categoryView:" + Thread.currentThread().getName());
            result.put("categoryView", categoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            System.out.println("price:" + Thread.currentThread().getName());
            result.put("price", skuPrice);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(t -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySkuList = productFeignClient.
                    getSpuSaleAttrListCheckBySku(skuId, t.getSpuId());
            System.out.println("spuSaleAttrListCheckBySkuList:" + Thread.currentThread().getName());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySkuList);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(t -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(t.getSpuId());
            System.out.println("valuesSkuJson:" + Thread.currentThread().getName());
            result.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //热度  开启此线程之后 不需要等结果
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);

        CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture,
                priceCompletableFuture,spuSaleAttrListCompletableFuture,valuesSkuJsonCompletableFuture
                ).join();

        return result;
    }
}
